package lua.compiler;

public enum CtsType {
	INT32 {
		public String toString() {
	        return "int32";
	    }
	},
	
	FLOAT32 {
		public String toString() {
	        return "float32";
	    }
	},
	
	STRING {
		public String toString() {
	        return "string";
	    }
	},
	
	OBJECT {
		public String toString() {
	        return "object";
	    }
	},
	
	VOID {
		public String toString() {
	        return "void";
	    }
	};
	
	public void ConvertTo(CtsType ctsType, Function function) {
		if (ctsType != this) {
			switch (ctsType) {
				case INT32:
					function.addInstruction("conv.i4");
					break;
				case FLOAT32:
					function.addInstruction("conv.r4");
					break;
				case STRING:
					switch (this) {
						case INT32:
							function.addInstruction("box [mscorlib]System.Int32");
							break;
						case FLOAT32:
							function.addInstruction("box [mscorlib]System.Single");
							break;
					}
					break;
			}
		}
	}
	
	public void ConvertToAtPos(CtsType ctsType, int pos, Function function) {
		if (ctsType != this) {
			switch (ctsType) {
				case INT32:
					function.insertInstruction(pos, "conv.i4");
					break;
				case FLOAT32:
					function.insertInstruction(pos, "conv.r4");
					break;
				case STRING:
					switch (this) {
						case INT32:
							function.insertInstruction(pos, "box [mscorlib]System.Int32");
							break;
						case FLOAT32:
							function.insertInstruction(pos, "box [mscorlib]System.Single");
							break;
					}
					break;
			}
		}
	}
	
	
	public boolean isNumeric() {
		return this == CtsType.INT32 || this == CtsType.FLOAT32;
	}
	
	public static CtsType resolveNumeric(CtsType t1, CtsType t2) {
		return (t1 == CtsType.INT32 && t2 == CtsType.INT32) ? CtsType.INT32 : CtsType.FLOAT32; 
	}
}
